package com.outsourcing.bottle.ui;

import java.util.ArrayList;

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.ui.fragment.CityFragment;
import com.outsourcing.bottle.ui.fragment.CountryFragment;
import com.outsourcing.bottle.ui.fragment.GlobalFragment;
import com.outsourcing.bottle.ui.fragment.NearByFragment;
import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TabPageIndicator;
import com.viewpagerindicator.TitleProvider;
/**
 * 
 * @author 06peng
 *
 */
public class ChooseLocationTabsActivity extends BasicActivity {

	LoactionTabsAdapter mAdapter;
	ViewPager mPager;
	PageIndicator mIndicator;
	private ImageView back;
	
	public String[] choices;
	public Location location;
	public String reference;
	public String type;
	public LoactionTabsAdapter adapter;
	public int currentItem;
	public int bottleId;
	private int locationSelect;
	
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.choose_location_tabs);
		networkLocat();
		type = getIntent().getStringExtra("type");
		bottleId = getIntent().getIntExtra("bottleId", 0);
		locationSelect = getIntent().getIntExtra("locationSelect", 1);
		initTabsFragment();
	}
	
	private void initTabsFragment() {
		mAdapter = new LoactionTabsAdapter(getSupportFragmentManager(), this);
		Bundle bundle = new Bundle();
		if (locationSelect == 1) {
			choices = new String[1];
			choices[0] = getString(R.string.choose_location_nearby);
			
			bundle.putInt("currentItem", 0);
			mAdapter.addFragment(choices[0], NearByFragment.class, bundle);
		} else if (locationSelect == 2) {
			choices = new String[2];
			choices[0] = getString(R.string.choose_location_nearby);  
			choices[1] = getString(R.string.choose_location_city);  
			
			bundle.putInt("currentItem", 0);
			mAdapter.addFragment(choices[0], NearByFragment.class, bundle);
			Bundle bundle1 = new Bundle();
			bundle1.putInt("currentItem", 1);
			mAdapter.addFragment(choices[1], CityFragment.class, bundle1);
		} else if (locationSelect == 3) {
			choices = new String[3];
			choices[0] = getString(R.string.choose_location_nearby);  
			choices[1] = getString(R.string.choose_location_city);  
			choices[2] = getString(R.string.choose_location_country);
			
			bundle.putInt("currentItem", 0);
			mAdapter.addFragment(choices[0], NearByFragment.class, bundle);
			Bundle bundle1 = new Bundle();
			bundle1.putInt("currentItem", 1);
			mAdapter.addFragment(choices[1], CityFragment.class, bundle1);
			Bundle bundle2 = new Bundle();
			bundle2.putInt("currentItem", 2);
			mAdapter.addFragment(choices[2], CountryFragment.class, bundle2);
		} else if (locationSelect == 4) {
			choices = new String[4];
			choices[0] = getString(R.string.choose_location_nearby);  
			choices[1] = getString(R.string.choose_location_city);  
			choices[2] = getString(R.string.choose_location_country);
			choices[3] = getString(R.string.choose_location_global);
			
			bundle.putInt("currentItem", 0);
			mAdapter.addFragment(choices[0], NearByFragment.class, bundle);
			Bundle bundle1 = new Bundle();
			bundle1.putInt("currentItem", 1);
			mAdapter.addFragment(choices[1], CityFragment.class, bundle1);
			Bundle bundle2 = new Bundle();
			bundle2.putInt("currentItem", 2);
			mAdapter.addFragment(choices[2], CountryFragment.class, bundle2);
			Bundle bundle3 = new Bundle();
			bundle3.putInt("currentItem", 3);
			mAdapter.addFragment(choices[3], GlobalFragment.class, bundle3);
		}
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);

		mIndicator = (TabPageIndicator) findViewById(R.id.indicator);
		mIndicator.setViewPager(mPager);
		
		back = (ImageView) findViewById(R.id.topbar_back);
		back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	public static class LoactionTabsAdapter extends FragmentPagerAdapter implements TitleProvider  {

    	private Context mContext;
		private final ArrayList<FragmentInfo> fragments = new ArrayList<FragmentInfo>();
		
		protected static final class FragmentInfo {
			private final String tag;
            private final Class<?> clss;
            private final Bundle args;

            protected  FragmentInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }
    	
		public LoactionTabsAdapter(FragmentManager fm, Context context) {
			super(fm);
			this.mContext = context;
		}
		
		public void addFragment(String tag, Class<?> clss, Bundle args) {
			FragmentInfo fragmentInfo = new FragmentInfo(tag, clss, args);
			fragments.add(fragmentInfo);
		}

		@Override
		public Fragment getItem(int arg0) {
			FragmentInfo fragmentInfo = fragments.get(arg0);
			return Fragment.instantiate(mContext, fragmentInfo.clss.getName(), fragmentInfo.args);
		}

		@Override
		public int getCount() {
			return fragments.size();
		}

		@Override
		public String getTitle(int position) {
			return fragments.get(position).tag;
		}
    }
	
}
